<template>
    <div class="app-container">



        <h2 style="margin: auto;text-align: center;">质检申请</h2>
        <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
            <h3>基本信息</h3>
            <el-row>
                <el-col :span="5">
                    <el-form-item label="关联采购合同">
                        <el-input v-model="form.cCom" clearable :style="{ width: '100%' }"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="供应商名称">
                        <el-input v-model="form.cName" clearable :style="{ width: '100%' }">
                        </el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="5">
                    <el-form-item label="制单人员">
                        <el-input v-model="form.createPeo" clearable :style="{ width: '100%' }" :disabled="true">
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="5">

                    <el-form-item label="制单日期">


                        <el-date-picker v-model="form.cTime" type="date" placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="5">
                    <el-form-item label="关联项目">
                        <el-input v-model="form.cConP" clearable :style="{ width: '100%' }"></el-input>
                    </el-form-item>
                </el-col>

            </el-row>
            <el-row>

                <el-col :span="18">
                    <el-form-item label="备注">
                        <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.cDesc">
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>

        </el-form>
        <!-- <div slot="footer">
            <el-button size="small" type="text" @click="cancel">取消</el-button>
            <el-button size="small" v-loading="formLoading" type="primary" @click="save">确认</el-button>
        </div> -->
        <div>
            <h2>质检申请明细</h2>
            <el-button size="small" @click="addRow">新增</el-button>
        </div>
        <!--设置的表单-->
        <el-form :model="studentData" ref="data" label-width="auto" border>
            <el-table border :header-cell-style="{ 'text-align': 'center' }" :cell-style="{ 'text-align': 'center' }"
                :data="studentData" ref="table" style="width: 100%">

                <el-table-column align="center" label="质检主题">
                    <template slot-scope="scope">
                        <!--表格里面嵌套表单-->
                        <el-form-item :prop="scope.$index + '.cdTitle'"
                            :rules="{ required: true, message: '质检主题不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].cdTitle" autocomplete="off" size="small"
                                placeholder="质检主题"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>

                <el-table-column align="center" label="质检编号">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdNum'"
                            :rules="{ required: true, message: '质检编号不能为空', trigger: 'blur' }">

                            <el-input v-model="studentData[scope.$index].cdNum" autocomplete="off" size="small"
                                placeholder="质检编号"></el-input>

                        </el-form-item>
                    </template>
                </el-table-column>

                <el-table-column align="center" label="质检人员">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdPeo'"
                            :rules="{ required: true, message: '质检人员不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].cdPeo" autocomplete="off" size="small"
                                placeholder="质检人员"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>

                <el-table-column align="center" label="产品名称">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdpName'"
                            :rules="{ required: true, message: '产品名称不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].cdpName" autocomplete="off" size="small"
                                placeholder="产品名称"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="产品编号">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdpNum'"
                            :rules="{ required: true, message: '产品编号不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].cdpNum" autocomplete="off" size="small"
                                placeholder="产品编号"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="规格型号">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdsNum'"
                            :rules="{ required: true, message: '规格型号不能为空', trigger: 'blur' }">


                            <el-input v-model="studentData[scope.$index].cdsNum" autocomplete="off" size="small"
                                placeholder="规格型号"></el-input>


                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="单位">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdUnit'"
                            :rules="{ required: true, message: '单位不能为空', trigger: 'blur' }">

                            <el-input v-model="studentData[scope.$index].cdUnit" autocomplete="off" size="small"
                                placeholder="单位"></el-input>

                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="单价">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdPrice'"
                            :rules="{ required: true, message: '单价不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].cdPrice" autocomplete="off" size="small"
                                placeholder="单价"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>

                <el-table-column align="center" label="总金额">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdaNum'"
                            :rules="{ required: true, message: '总金额不能为空', trigger: 'blur' }">



                            <el-input v-model="studentData[scope.$index].cdaNum" autocomplete="off" size="small"
                                placeholder="总金额"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="到货数量">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdaNum'"
                            :rules="{ required: true, message: '到货数量不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].cdaNum" autocomplete="off" size="small"
                                placeholder="到货数量" type='number'></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="已申请质检数量">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdQuesNum'"
                            :rules="{ required: true, message: '已申请质检数量不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].cdQuesNum" autocomplete="off" size="small"
                                placeholder="已申请质检数量"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="采购合同数量">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdShopNum'"
                            :rules="{ required: true, message: '采购合同数量不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].cdShopNum" autocomplete="off" size="small"
                                placeholder="采购合同数量"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="批号">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdph'"
                            :rules="{ required: true, message: '批号不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].cdph" autocomplete="off" size="small"
                                placeholder="批号"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="生产日期">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdbTime'"
                            :rules="{ required: true, message: '生产日期不能为空', trigger: 'blur' }">


                            <el-date-picker v-model="studentData[scope.$index].cdbTime" type="date" placeholder="生产日期">
                            </el-date-picker>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="有效日期">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdTime'"
                            :rules="{ required: true, message: '有效日期不能为空', trigger: 'blur' }">
                            <el-date-picker v-model="studentData[scope.$index].cdTime" type="date" placeholder="有效日期">
                            </el-date-picker>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="到货日期">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdaTime'"
                            :rules="{ required: true, message: '到货日期不能为空', trigger: 'blur' }">


                            <el-date-picker v-model="studentData[scope.$index].cdaTime" type="date" placeholder="到货日期">
                            </el-date-picker>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="包装方式">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdPage'"
                            :rules="{ required: true, message: '包装方式不能为空', trigger: 'blur' }">
                            <el-select v-model="studentData[scope.$index].cdPage" placeholder="请选择">
                                <el-option label="纸箱包装" value="纸箱包装"></el-option>
                                <el-option label="塑料包装" value="塑料包装"></el-option>
                                <el-option label="常规包装" value="常规包装"></el-option>
                                <el-option label="其他" value="其他"></el-option>
                            </el-select>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="备注">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.cdDesc'"
                            :rules="{ required: true, message: '备注不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].cdDesc" autocomplete="off" size="small"
                                placeholder="备注"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>






                <el-table-column fixed="right" label="操作" width="100">
                    <template slot-scope="scope">
                        <el-button @click="handleDeleteRow(studentData[scope.$index])" type="text"
                            size="small">删除</el-button>
                    </template>
                </el-table-column>

            </el-table>
        </el-form><br />
        <div slot="footer">
            <el-button size="small" type="text" @click="cancel">取消</el-button>
            <el-button size="small" v-loading="formLoading" type="primary" @click="save">确认</el-button>
        </div>
    </div>


    </div>
</template>
<script>
import Pagination from "@/components/Pagination";
import permission from "@/directive/permission/index.js";
import { cloneDeep } from "lodash";


export default {
    name: 'Book',
    components: {
        Pagination
    },
    directives: {
        permission
    },
    props: [],
    data() {
        return {
            studentData: [],
            form: {
                id: null,
                cCom: null,
                cName: null,
                createPeo: '方可欣',
                cTime: Date.now(),
                cConP: null,
                cDesc: null,
                createOrUpdateCheckQDto: []

            },


            rules: {
                name: [{
                    required: true,
                    message: '请输入名称',
                    trigger: 'blur'
                }],
                description: [{
                    required: true,
                    message: '请输入描述',
                    trigger: 'blur'
                }],
                price: [{
                    required: true,
                    message: '价格',
                    trigger: 'blur'
                }],
            },

            list: [],
            totalCount: 0,
            listLoading: true,
            formLoading: false,
            listQuery: {
                Filter: '',
                Sorting: '',
                SkipCount: 0,
                MaxResultCount: 10
            },
            page: 1,
            dialogFormVisible: false,
            multipleSelection: [],
            formTitle: '',
            isEdit: false,
        }
    },
    computed: {},
    watch: {},
    created() {
        this.getList()
    },
    mounted() { },
    methods: {
        /**
    * 新增行
    */
        addRow() {
            let index = this.studentData.length;
            this.studentData.push({

                cdTitle: null,
                cdNum: null,
                cdPeo: null,
                cdpName: null,
                cdpNum: null,
                cdsNum: null,
                cdUnit: null,
                cdPrice: null,
                cdaNum: null,
                cdQuesNum: null,
                cdShopNum: null,
                cdph: null,
                cdbTime: null,
                cdTime: null,
                cdaTime: null,
                cdPage: null,
                cdDesc: null,

            });
        },

        /**
         * 删除行
         * @param row
         */
        handleDeleteRow(row) {
            let datas = this.studentData;
            for (var i = 0; i < datas.length; i++) {
                if (datas[i].key == row.key) {
                    datas.splice(i, 1);
                }
            }
        },

        /**
         * 提交
         */
        submit() {
            this.$refs["data"].validate(valid => {
                //valid为true，表示表单都已经验证通过，若为false，说明存在表单验证失败
                if (valid) {
                    save(this.studentData).then(response => {
                        this.$message({
                            message: '提交成功',
                            type: 'success'
                        });
                    });
                }
            });
        },

        /**
         * 重置
         */
        resetForm() {
            let datas = this.studentData;
            for (var i = 0; i < datas.length; i++) {
                datas[i].name = '';
                datas[i].age = '';
                datas[i].sex = '';
            }
        },

        getListStatus(items, ids) {
            this.$axios.posts('api/business/workflow/node-status', ids).then(response => {
                items.forEach(element => { response.items.filter((i) => { if (i.entityId === element.id) { element.status = i.status; element.nodeId = i.nodeId; element.nodeStatus = i.nodeName } }) })
                this.list = items
                this.listLoading = false;
            });
        },
        getList() {
            this.listLoading = true;
            this.listQuery.SkipCount = (this.page - 1) * this.listQuery.MaxResultCount;
            this.$axios.gets('/api/business/checkq', this.listQuery).then(response => {
                this.totalCount = response.totalCount;
                this.getListStatus(response.items, response.items.map(_ => _.id))
            });
        },
        fetchData(id) {
            this.$axios.gets('/api/business/checkq/' + id).then(response => {
                this.form = response;
            });
        },
        handleFilter() {
            this.page = 1;
            this.getList();
        },
        handleCreate() {
            this.formTitle = '新增图书';
            this.isEdit = false;
            this.dialogFormVisible = true;
        },
        handleDelete(row) {
            var params = [];
            let alert = '';
            if (row) {
                params.push(row.id);
                alert = row.name;
            }
            else {
                if (this.multipleSelection.length === 0) {
                    this.$message({
                        message: '未选择',
                        type: 'warning'
                    });
                    return;
                }
                this.multipleSelection.forEach(element => {
                    let id = element.id;
                    params.push(id);
                });
                alert = '选中项';
            }
            this.$confirm('是否删除' + alert + '?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.posts('/api/business/checkq/delete', params).then(response => {
                    this.$notify({
                        title: '成功',
                        message: '删除成功',
                        type: 'success',
                        duration: 2000
                    });
                    this.getList();
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        handleCheck() {
            if (this.multipleSelection.length === 0) {
                this.$message({
                    message: '未选择',
                    type: 'warning'
                });
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message({
                    message: '暂不支持批量审核',
                    type: 'warning'
                });
                return;
            }
            this.$confirm('是否审核所选项?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.puts('api/business/workflow/do/' + this.multipleSelection[0].id, { data: JSON.stringify(this.multipleSelection[0]) }).then(response => {
                    this.$notify({
                        title: '成功',
                        message: '审核成功',
                        type: 'success',
                        duration: 2000
                    });
                    this.getList();
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消审核'
                });
            });
        },
        findFlow(row) {
            this.$router.push({ path: "/tool/flowDisplay/" + row.nodeId });
        },
        handleUpdate(row) {
            this.formTitle = '修改图书';
            this.isEdit = true;
            if (row) {
                this.fetchData(row.id);
                this.dialogFormVisible = true;
            }
            else {
                if (this.multipleSelection.length != 1) {
                    this.$message({
                        message: '编辑必须选择单行',
                        type: 'warning'
                    });
                    return;
                }
                else {
                    this.fetchData(this.multipleSelection[0].id);
                    this.dialogFormVisible = true;
                }
            }
        },
        save() {
            //数组与对象转换
            let goodList = [];
            this.studentData.forEach(element => {
                goodList.push({
                    id: element.id,
                    cdTitle: element.cdTitle,
                    cdNum: element.cdNum,
                    cdPeo: element.cdPeo,
                    cdpName: element.cdpName,
                    cdpNum: element.cdpNum,
                    cdsNum: element.cdsNum,
                    cdUnit: element.cdUnit,
                    cdPrice: element.cdPrice,
                    cdaNum: element.cdaNum,
                    cdQuesNum: element.cdQuesNum,
                    cdShopNum: element.cdShopNum,
                    cdph: element.cdph,
                    cdbTime: element.cdbTime,
                    cdTime: element.cdTime,
                    cdaTime: element.cdaTime,
                    cdPage: element.cdPage,
                    cdDesc: element.cdDesc,
                });
            });
            this.form.createOrUpdateCheckQDto = goodList;

            console.log(this.form)





            this.$refs.form.validate(valid => {
                if (valid) {
                    this.formLoading = true;
                    this.form.roleNames = this.checkedRole;
                    if (this.isEdit) {
                        this.$axios.posts('/api/business/checkq/data-post', this.form).then(response => {
                            this.formLoading = false;
                            this.$notify({
                                title: '成功',
                                message: '更新成功',
                                type: 'success',
                                duration: 2000
                            });
                            this.dialogFormVisible = false;
                            this.getList();
                        }).catch(() => {
                            this.formLoading = false;
                        });
                    }
                    else {
                        this.$axios.posts('/api/business/checkq/data-post', this.form).then(response => {
                            this.formLoading = false;
                            this.$notify({
                                title: '成功',
                                message: '新增成功',
                                type: 'success',
                                duration: 2000
                            });
                            this.dialogFormVisible = false;
                            this.getList();
                        }).catch(() => {
                            this.formLoading = false;
                        });
                    }
                }
            });
        },
        sortChange(data) {
            const {
                prop,
                order
            } = data;
            if (!prop || !order) {
                this.handleFilter();
                return;
            }
            this.listQuery.Sorting = prop + ' ' + order;
            this.handleFilter();
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        handleRowClick(row, column, event) {
            this.$refs.multipleTable.clearSelection();
            this.$refs.multipleTable.toggleRowSelection(row);
        },
        cancel() {
            this.form = Object.assign({}, defaultForm);
            this.dialogFormVisible = false;
            this.$refs.form.clearValidate();
        },
    }
}

</script>
<style></style>
